<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/29 0029
 * Time: 上午 9:12
 */

namespace app\admin\model;


use app\admin\model\Base;
use think\Db;


class Menu extends Base
{

    protected $table = "authority_menu";
    protected $base_arr = [
        'table' => 'authority_menu',
        'field' => 'menuname,upmenuid,menuurl,state',
        'primary_key' => 'id'
        ];

    //附属的表单
    protected $attach_arr =  [
        [
        'table' => 'authority_menu',
        'field' => 'menuname',
        'primary_key' => 'id',
        'base_table_field' => 'upmenuid'
        ],

    ];

    /**
     * 获取字段对应的数组
     * */
    public function getDataByMysql(){
        $data = [
            't_name' => 'authority_menu',
            'model' => '菜单',
            'menuname' => '功能名称',
            'upmenuid' => '上级菜单',
            'menuurl' => '链接地址',
            'state' => '{"0":"功能禁用","1":"功能启用","2":"功能删除"}'
        ];
         return $data;
    }


    /**
     * 获取日志的访问方法
    **/
    public static function getMenuAdminLog($list_action='',$data=[],$userId,$admin_type){
        $model = new Menu();
        $actionName = strtolower(ACTION_NAME);
        $controller = strtolower(CONTROLLER_NAME);
        $old_data = [];
        $type = '';



        switch($actionName){
            case "menu":
                Base::save_admin_log($userId,$model,'check');
                break;
            case "addmenu":
                $type = 'add';
                if($model->request->post()){
                    if ($data["upmenuid"] == 0) {
                        // 上级功能为0时表示新增的是根目录
                        $data["upmenuids"] = "0,";
                        $data["level"] = 1;

                        // 获取排序阶层
                        $sqlsort = "select * from authority_menu where `sortlevel` like '%,' and (length(`sortlevel`) - length(replace(sortlevel,',',''))) = 1 order by sortlevel desc";
                        $listsort = Db::query($sqlsort);

                        if ($listsort == null) {
                            // 没有子阶层的时候从0开始
                            $data["sortlevel"] = "0,";
                        } else {
                            // 有子阶层取最后一个阶层的最后一位数字+1
                            $temp = explode(",", $listsort[0]["sortlevel"]);
                            $data["sortlevel"] = ($temp[0] + 1) . ",";
                        }


                    } else {
                        // 新增子目录功能
                        $list = Db::query("select * from authority_menu where id=?", [$data["upmenuid"]]);
                        if ($list == null) {
                            //echo "<script>alert('未查到上级功能！');window.location.href = '{$this->nowpath}}menu';</script>";
                            return ['flag'=>'error','msg'=>'未能找到上一级','url'=>'authority/menu'];
                        } else {
                            $data["upmenuids"] = $list[0]["upmenuids"] . $data["upmenuid"] . ",";
                            $data["level"] = $list[0]["level"] + 1;

                            // 获取排序阶层
                            $sqlsort = "select * from authority_menu where `sortlevel` like ? and (length(`sortlevel`) - length(replace(sortlevel,',',''))) = ? order by sortlevel desc";
                            $listsort = Db::query($sqlsort, [$list[0]["sortlevel"] . "%,", $data["level"]]);
                            if ($listsort == null) {
                                // 没有子阶层的时候从0开始
                                $data["sortlevel"] = $list[0]["sortlevel"] . "0,";
                            } else {
                                // 有子阶层取最后一个阶层的最后一位数字+1
                                $temp = explode(",", $listsort[0]["sortlevel"]);
                                $data["sortlevel"] = $list[0]["sortlevel"] . ($temp[count($temp) - 2] + 1) . ",";
                            }


                        }
                    }
                }
                break;
            case "editmenu":

                $type = 'edit';
                if ($model->request->isPost()) {

                    $sqlsort = "select * from authority_menu where `sortlevel` like '%,' and (length(`sortlevel`) - length(replace(sortlevel,',',''))) = 1 order by sortlevel desc";
                    $listsort = Db::query($sqlsort);
                    $data["upmenuids"] = $listsort[0]["upmenuids"] . $data["upmenuid"] . ",";
                    $data["level"] = $listsort[0]["level"] + 1;

                    //获取原先数据
                    $old_data = Base::getOneOldDataByTables($data['id'],$model->base_arr,$model->attach_arr);
                }

                break;
                case "deletemenu":
                    $type = 'del';
                    if ($model->request->isPost()) {
                        Db::execute("update authority_menu set state = '2' where id = ? OR upmenuids like ?", [$data["id"], "%" . $data["id"] . ",%"]);
                        $data['state'] = 2;
                        $old_data = ['state' => 1];
                    }
                    break;

            }

            if($model->request->isPost()){
                $result =  Base::operaTable($model,$userId,$type,$data,$controller.'/'.$list_action,$old_data,$model->attach_arr,'id',$admin_type);
                return $result;
            }


          return true;


    }






}
